package com.lx.pc.restful.goodssupply;

import java.math.BigInteger;
import java.util.HashMap;
import java.util.List;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.alibaba.fastjson.JSON;
import com.lx.pc.domain.vo.category.CategoryVo;
import com.lx.pc.domain.vo.goodssupply.GoodssupplyVo;
import com.lx.pc.domain.vo.goodssupplyimg.GoodssupplyimgVo;
import com.lx.pc.domain.vo.sysarea.SysareaVo;
import com.lx.pc.restful.BaseController;
import com.lx.pc.service.category.ICategoryService;
import com.lx.pc.service.goodssupply.IGoodssupplyService;
import com.lx.pc.service.goodssupplyimg.IGoodssupplyimgService;
import com.lx.pc.service.sysarea.ISysareaService;
import com.lx.pc.utils.DateUtil;
import com.lx.pc.utils.OmsPage;

@Controller
@RequestMapping(name = "goodssupply")
public class GoodsSupplyController extends BaseController {
	
	/**
	 * 现货
	 */
	@Resource(name = "goodssupplyService")
	private IGoodssupplyService goodssupplyService;

	/**
	 * 现货图片
	 */
	@Resource(name = "goodssupplyimgService")
	private IGoodssupplyimgService goodssupplyimgService;

	/**
	 * 地区
	 */
	@Resource(name = "sysareaService")
	private ISysareaService sysareaService;

	/**
	 * 类目
	 */
	@Resource(name = "categoryService")
	private ICategoryService categoryService;

	@RequestMapping(value = "/toGoodsSupplyList.do")
	public ModelAndView toGoodsSupplyList(HttpServletRequest request) {

		ModelAndView mv = getModelAndView("/goodssupply/goodsSupply_list", request);

		// 类目
		CategoryVo categoryVo = new CategoryVo();
		categoryVo.setType(1);
		categoryVo.setActive((short) 1);
		categoryVo.setStatus(1);
		List<CategoryVo> categoryList = categoryService.findByCategoryList(categoryVo);

		// 查询地区
		SysareaVo sysarea = new SysareaVo();
		sysarea.setParentCode("0");
		List<SysareaVo> sysareaList = sysareaService.findBySysareaList(sysarea);

		mv.addObject("sysarea", sysareaList);
		mv.addObject("category", categoryList);
		return mv;
	}

	@RequestMapping("/goodssupplyList.do")
	@ResponseBody
	public String goodssupplyList(GoodssupplyVo v, Integer page, Integer size) {
		HashMap<String, Object> result = new HashMap<String, Object>();
		OmsPage<GoodssupplyVo> pages = goodssupplyService.findByGoodssupply(v, page, size);
		for (GoodssupplyVo goodssupplyVo : pages.getResult()) {
			//有效期
			goodssupplyVo.setDeadline(DateUtil.getContrastDayTime(goodssupplyVo.getDeadline()));
		}

		result.put("goodssupplyVo", pages);
		return JSON.toJSONString(result);

	}

	@RequestMapping("/goodsSupplyoffer.do")
	public ModelAndView findGoodssupplyoffer(String id, HttpServletRequest request, HttpServletResponse response) {
		ModelAndView mv = new ModelAndView("/goodssupply/goodsSupply_offer");
		BigInteger bId = new BigInteger(id);
		GoodssupplyVo goodssupplyVo = goodssupplyService.findByGoodssupplyId(bId);
		mv.addObject("goodssupplyVo", goodssupplyVo);
		return mv;

	}

	@RequestMapping("/goodsSupplydetails.do")
	public ModelAndView findGoodsSupplydetails(String id) {
		ModelAndView mv = new ModelAndView("/goodssupply/goodsSupply_details");
		BigInteger bId = new BigInteger(id);
		GoodssupplyVo goodssupplyVo = goodssupplyService.findByGoodssupplyId(bId);

		mv.addObject("goodssupplyVo", goodssupplyVo);
		GoodssupplyimgVo entity = new GoodssupplyimgVo();
		entity.setGoodsSupplyId(bId);
		List<GoodssupplyimgVo> goodssupplyImg = goodssupplyimgService.findByGoodssupplyImg(entity);
		mv.addObject("goodssupplyImg", goodssupplyImg);
		return mv;

	}

}
